﻿using System;
using Lenic.DI.Lifetimes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lenic.DI.Tests
{
    [TestClass]
    public class BasicTest
    {
        [TestMethod]
        public void TestRegister1()
        {
            IContainer container = new Container();

            container.Register(null, c => new Person() { Id = 3, Name = "张三" });
        }

        [TestMethod]
        public void TestRegister2()
        {
            IContainer container = new Container();

            container.Register<IPerson>(null, c => new Person() { Id = 3, Name = "张三" });
        }

        [TestMethod]
        public void TestRegisterAndResolve1()
        {
            IContainer container = new Container();

            container.Register(null, c => new Person() { Id = 3, Name = "张三" });

            var p = container.Resolve<Person>();

            Assert.IsInstanceOfType(p, typeof(Person));
            Assert.AreEqual(3, p.Id);
            Assert.AreEqual("张三", p.Name);
        }

        [TestMethod]
        public void TestRegisterAndResolve11()
        {
            IContainer container = new Container();

            container.Register(new SingleLifetime(), c => new Person() { Id = 4, Name = "李四" });

            var p = container.Resolve<Person>();

            Assert.IsInstanceOfType(p, typeof(Person));
            Assert.AreSame(p, container.Resolve<Person>());
            Assert.AreEqual(4, p.Id);
            Assert.AreEqual("李四", p.Name);
        }

        [TestMethod]
        public void TestRegisterAndResolve12()
        {
            IContainer container = new Container();

            container.Register(null, c => new Person() { Id = 3, Name = "张三" })
                     .RegisterNamed(null, "x", c => new Person() { Id = 4, Name = c.Resolve<Person>().Name });

            var p = container.ResolveNamed<Person>("x");

            Assert.IsInstanceOfType(p, typeof(Person));
            Assert.AreEqual(4, p.Id);
            Assert.AreEqual("张三", p.Name);
        }

        [TestMethod]
        public void TestRegisterAndResolve131()
        {
            IContainer container = new Container();

            container.Register<Person, int>((c, arg1) => new Person() { Id = arg1, Name = "张三" });

            var p = container.Resolve<Person, int>(4);

            Assert.IsInstanceOfType(p, typeof(Person));
            Assert.AreEqual(4, p.Id);
            Assert.AreEqual("张三", p.Name);
        }

        [TestMethod]
        public void TestRegisterAndResolve132()
        {
            IContainer container = new Container();

            container.Register<Person, int, string>((c, arg1, arg2) => new Person() { Id = arg1, Name = arg2 });

            var p = container.Resolve<Person, int, string>(4, "李四");

            Assert.IsInstanceOfType(p, typeof(Person));
            Assert.AreEqual(4, p.Id);
            Assert.AreEqual("李四", p.Name);
        }

        [TestMethod]
        public void TestRegisterAndResolve2()
        {
            IContainer container = new Container();

            container.Register<IPerson>(null, c => new Person() { Id = 3, Name = "张三" });

            var p = container.Resolve<IPerson>();

            Assert.IsInstanceOfType(p, typeof(IPerson));
            Assert.AreEqual(3, p.Id);
            Assert.AreEqual("张三", p.Name);
        }
    }
}